//
//  PatientService.m
//  Nurse
//
//  Created by 夏 申频 on 14-1-5.
//  Copyright (c) 2014年 Honeywell. All rights reserved.
//

#import "OrdersService.h"
#import "OperatorService.h"

NSString* const PRODUCEBEDSFORYZ    = @"DY_OUT_ZY_YZXX";
NSString* const PRODUCEDUTIESFORPID = @"DY_OUT_ZY_TASK";
NSString* const PRODUCEEXECUTEDUTYFORID = @"DY_INE_Update_TASK";
NSString* const PRODUCEEXECUTEDUTYFORBQ =@"DY_OUT_ZY_TASK_BQ";
NSString* const PRODUCEEXECUTEDUTYFORJQ =@"DY_OUT_JQ";

@interface OrdersService()
@property (nonatomic,strong) NSURL* url;
@end



@implementation OrdersService

-(OrdersService*)init{
    self= [super init];
    if (self) {
        self.url = [[DataEngine sharedInstance]urlForPath:@"/his/procedure"];
    }
    return self;
}

+(OrdersService*) sharedInstance{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[OrdersService alloc] init];
    });
    return _sharedObject;
}

//归并任务
-(NSMutableArray*)processTasksData:(NSMutableArray*)rawData{
    NSMutableDictionary* mainDict = [NSMutableDictionary dictionary];
    
    for (NSDictionary* task in rawData) {
        NSString* zyzbh = [task valueForKey:@"主医嘱编号"];
        NSString* rwrqsj = [task valueForKey:@"任务日期时间"];
        //NSString* rwsj = [task valueForKey:@"任务时间"];
        NSString* keyValue = [NSString stringWithFormat:@"%@%@",zyzbh,rwrqsj];
        NSLog(@"eky Value:%@",keyValue);
        if (![[mainDict allKeys]containsObject:keyValue]) {
            NSMutableDictionary* child = [NSMutableDictionary dictionary];
            [child setValue:zyzbh forKey:@"name"];
            
            NSString* type = [task valueForKey:@"任务种类"];
            [child setValue:type forKey:@"type"];
            
            NSString* zyh = [task valueForKey:@"住院号"];
            [child setValue:zyh forKey:@"zyh"];

            
            NSString* title = [task valueForKey:@"任务内容"];
            [child setValue:title forKey:@"title"];
            NSString* xm = [task valueForKey:@"姓名"];
            [child setValue:xm forKey:@"xm"];
            NSMutableArray* childList = [NSMutableArray array];
            [child setValue:childList forKey:@"list"];
            
            NSString* dateTime = [task valueForKey:@"任务日期时间"];
            //[NSString stringWithFormat:@"%@%@",[task valueForKey:@"任务日期"],[task valueForKey:@"任务时间"]];
            [child setValue:dateTime forKey:@"dateTime"];
            
            NSNumber* status = [task valueForKey:@"任务执行状态"];
            [child setValue:status forKey:@"status"];
            
            [child setValue:@"0" forKey:@"scanStatus"];
            
            [child setValue:keyValue forKey:@"keyValue"];
            
            [child setValue:[NSNumber numberWithBool:FALSE] forKey:@"isOpen"];
            
            [mainDict setObject:child forKey:keyValue];
        }
        
        NSMutableDictionary* child = [mainDict valueForKey:keyValue];
        NSMutableArray* childList =[child valueForKey:@"list"];
        [childList addObject:task];
    }
    
    NSArray* resultTask =  [[mainDict allValues]sortedArrayUsingComparator:^NSComparisonResult(NSDictionary* obj1, NSDictionary* obj2) {
        NSNumber* s1 = [obj1 valueForKey:@"status"];
        NSNumber* s2 = [obj2 valueForKey:@"status"];
        NSString* t1 = [obj1 valueForKey:@"dateTime"];
        NSString* t2 = [obj2 valueForKey:@"dateTime"];
        NSComparisonResult statusCompare = [s1 compare:s2];
        if (statusCompare != NSOrderedSame) {
            return statusCompare;
        }
        else{
            return [t1 compare:t2];
        }
    }];
    
    return resultTask;
    
}

-(void)queryOrdersForPatient:(NSString*)PatientID withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName=PRODUCEBEDSFORYZ;
    request.params = @{@"zyh":PatientID};
    request.successHandle = successHandler;
    request.failHandle = failHandler;
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];
}

-(void)queryDutiesForPatient:(NSString*)patientID withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName=PRODUCEDUTIESFORPID;
    request.params = @{@"tj":patientID};
    request.successHandle = successHandler;
    request.failHandle = failHandler;
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];
}

-(void)queryDutiesForBq:(NSString*)BQ withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName=PRODUCEEXECUTEDUTYFORBQ;
    //NSString *lb=@"2";
    request.params = @{@"tj":BQ};
                       //@"lb":lb};
    request.successHandle = successHandler;
    request.failHandle = failHandler;
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];
}

-(void)queryOrdersForJQ:(NSString *)Rq withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler
{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName=PRODUCEEXECUTEDUTYFORJQ;
    //NSString *lb=@"2";
    request.params = @{@"Rq":Rq};
    //@"lb":lb};
    request.successHandle = successHandler;
    request.failHandle = failHandler;
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];

}
-(void)executeDutyForID:(NSString*)dutyID withStatus:(NSString*)status withSuccessHandler:(SuccessHandler)successHandler andFailHandler:(FailHandler)failHandler;{
    ProcedureCallRequest* request = [[ProcedureCallRequest alloc]init];
    request.procedureName=PRODUCEEXECUTEDUTYFORID;
    NSLog(@"任务编号:%@",dutyID);
    
    NSString* gh = [[OperatorService sharedInstance].currentOperator valueForKey:@"工号"];
    request.params = @{@"任务编号"     : dutyID,
                       @"任务执行状态"  : status,
                       @"任务执行人"   :  gh};
    request.successHandle = successHandler;
    request.failHandle = failHandler;
    [[DataEngine sharedInstance]callProcedure:request forURL:self.url];

}
@end
